package com.wenju.liebiaodaojishi;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

class TimeDownLvAdapter extends RecyclerView.Adapter<TimeDownLvAdapter.ViewHolder> {
    List<TimeDownItem> lstTimeDownItems;
    private Handler mHandler;//线程调度，用来更新列表
    private Timer mTimer;

    public TimeDownLvAdapter(Context context, List<TimeDownItem> lstTimeDownItems) {
        this.lstTimeDownItems = lstTimeDownItems;
        mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        notifyItemChanged(msg.arg1, "update-time");
                        break;
                }
            }
        };
        if (mTimer == null) {
            mTimer = new Timer();
        }
        mTimer.schedule(new MyTask(), 0, 1000);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_common, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tv.setText(lstTimeDownItems.get(position).getName() + position);
        if(lstTimeDownItems.get(position).getExpirationTime() == 0){
            holder.tv2.setText("倒计时结束");
        }else{
            holder.tv2.setText("倒计时：" + lstTimeDownItems.get(position).getExpirationTime()+"s");
        }

    }

    @Override
    public int getItemCount() {
        return lstTimeDownItems.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv, tv2;

        ViewHolder(View view) {
            super(view);
            tv = view.findViewById(R.id.tv);
            tv2 = view.findViewById(R.id.tv2);
        }
    }

    class MyTask extends TimerTask {
        @Override
        public void run() {
            if (lstTimeDownItems.isEmpty()) {
                return;
            }
            int size = lstTimeDownItems.size();
            TimeDownItem bean = null;
            long totalTime;
            for (int i = 0; i < size; i++) {
                bean = lstTimeDownItems.get(i);
                totalTime = bean.getExpirationTime() - 1;
                if (totalTime >= 0) {
                    bean.setExpirationTime(totalTime);
                    Message message = mHandler.obtainMessage(1);
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }

            }
        }
    }
}
